package algorithms;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lizen
 */
public class CurrencyConverter {
    public static void test() {
        // 示例汇率表
        Map<String, Map<String, Double>> exchangeRates = new HashMap<>();
        exchangeRates.put("USD", new HashMap<>());
        exchangeRates.get("USD").put("EUR", 0.85);
        exchangeRates.get("USD").put("JPY", 110.0);

        exchangeRates.put("EUR", new HashMap<>());
        exchangeRates.get("EUR").put("USD", 1.18);
        exchangeRates.get("EUR").put("JPY", 130.0);

        exchangeRates.put("JPY", new HashMap<>());
        exchangeRates.get("JPY").put("USD", 0.0091);
        exchangeRates.get("JPY").put("EUR", 0.0077);

        exchangeRates.put("CNY", new HashMap<>());
        exchangeRates.get("CNY").put("USD", 0.15);

        // 示例：将 100 美元转换为欧元
        double amountUsd = 100;
        double amountEur = convertCurrency(amountUsd, "USD", "EUR", exchangeRates);
        System.out.println(amountUsd + " USD = " + amountEur + " EUR");

        // 示例：将 1000 日元转换为欧元
        double amountJpy = 1000;
        amountEur = convertCurrency(amountJpy, "JPY", "EUR", exchangeRates);
        System.out.println(amountJpy + " JPY = " + amountEur + " EUR");

        // 示例：将 1000 日元转换为欧元
        double amountCny = 1000;
        amountJpy = convertCurrency(amountCny, "CNY", "JPY", exchangeRates);
        System.out.println(amountCny + " CNY = " + amountJpy + " JPY");
    }

    /**
     * 多币种汇率转换
     *
     * @param amount        源金额
     * @param fromCurrency  源货币
     * @param toCurrency    目标货币
     * @param exchangeRates 汇率表（嵌套 Map 形式）
     * @return 转换后的金额
     * @throws IllegalArgumentException 如果无法找到汇率
     */
    public static double convertCurrency(double amount, String fromCurrency, String toCurrency,
                                         Map<String, Map<String, Double>> exchangeRates) {
        // 如果源货币和目标货币相同，直接返回
        if (fromCurrency.equals(toCurrency)) {
            return amount;
        }

        // 查找直接汇率
        if (exchangeRates.containsKey(fromCurrency) && exchangeRates.get(fromCurrency).containsKey(toCurrency)) {
            return amount * exchangeRates.get(fromCurrency).get(toCurrency);
        }

        // 查找间接汇率（通过中间货币）
        for (String intermediateCurrency : exchangeRates.keySet()) {
            if (exchangeRates.containsKey(fromCurrency) &&
                    exchangeRates.get(fromCurrency).containsKey(intermediateCurrency) &&
                    exchangeRates.containsKey(intermediateCurrency) &&
                    exchangeRates.get(intermediateCurrency).containsKey(toCurrency)) {
                // 转换公式：A -> B -> C
                return amount *
                        exchangeRates.get(fromCurrency).get(intermediateCurrency) *
                        exchangeRates.get(intermediateCurrency).get(toCurrency);
            }
        }

        // 如果找不到汇率，抛出异常
        throw new IllegalArgumentException("无法找到从 " + fromCurrency + " 到 " + toCurrency + " 的汇率");
    }
}
